package com.cgsd.heyu.ad;

import android.app.Activity;
import android.util.Log;

import com.android.library.utils.BeanUtils;
import com.apkfuns.logutils.LogUtils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.cgsd.heyu.api.AppConfig;
import com.cgsd.heyu.entity.AdContentEntity;
import com.qq.e.ads.interstitial2.UnifiedInterstitialAD;
import com.qq.e.ads.interstitial2.UnifiedInterstitialADListener;
import com.qq.e.ads.interstitial2.UnifiedInterstitialMediaListener;
import com.qq.e.comm.util.AdError;

import java.lang.ref.WeakReference;


/**
 * Description : 短视频广告
 * @author  : xiaoxing
 * 时间：2020/3/11 09
 * 邮箱：only_dream136@sina.com
 */
public class AdFullVideo {
    //穿山甲相关
    private TTFullScreenVideoAd mttFullVideoAd;
    private TTAdNative mTTAdNative;
    //广点通相关
    private UnifiedInterstitialAD unifiedInterstitialAD;

    private AdContentEntity adContentEntity;
    private WeakReference<Activity> weakReference;
    private AdListener adListener;

    public AdFullVideo(Activity activity, AdContentEntity entity,AdListener adListener){
        this.adListener = adListener;
        weakReference = new WeakReference<>(activity);
        this.adContentEntity = entity;

        if (BeanUtils.isEmpty(adContentEntity)) {
            return;
        }
        if (AdContentEntity.CHUANSHANJIA.equals(adContentEntity.platformType)) {
            mTTAdNative = TTAdSdk.getAdManager().createAdNative(weakReference.get());
        }
    }

    /**
     * 加载广告并显示
     */
    public void loadAd(){
        if (AdContentEntity.GUANGDIANTONG.equals(adContentEntity.platformType)) {
            adFullVideoG();
        } else {
            adFullVideoC();
        }
    }

    /**
     * 加载广点通开屏甲短视频广搞
     */
    private void adFullVideoG(){
        unifiedInterstitialAD = new UnifiedInterstitialAD(weakReference.get(), adContentEntity.advertisingId, new UnifiedInterstitialADListener() {
            @Override
            public void onADReceive() {
                if(adListener != null){
                    adListener.onSuccess();
                }
                unifiedInterstitialAD.showFullScreenAD(weakReference.get());
            }

            @Override
            public void onVideoCached() {
            }

            @Override
            public void onNoAD(AdError adError) {
                LogUtils.e("onError onNoAD"+""+adError.getErrorMsg());
                if (adListener != null){
                    adListener.onFail();
                }
            }

            @Override
            public void onADOpened() {
            }

            @Override
            public void onADExposure() {
            }

            @Override
            public void onADClicked() {
            }

            @Override
            public void onADLeftApplication() {
            }

            @Override
            public void onADClosed() {
                if (adListener != null){
                    adListener.onVideoComplete();
                }
            }
        });
        unifiedInterstitialAD.loadFullScreenAD();
        unifiedInterstitialAD.setMediaListener(new UnifiedInterstitialMediaListener() {
            @Override
            public void onVideoInit() {
            }

            @Override
            public void onVideoLoading() {
            }

            @Override
            public void onVideoReady(long l) {
            }

            @Override
            public void onVideoStart() {
            }

            @Override
            public void onVideoPause() {
            }

            @Override
            public void onVideoComplete() {
                if (adListener != null){
                    adListener.onVideoComplete();
                }
            }

            @Override
            public void onVideoError(AdError adError) {
            }

            @Override
            public void onVideoPageOpen() {
            }

            @Override
            public void onVideoPageClose() {
            }
        });
    }


    /**
     * 加载穿山开屏甲短视频广搞
     */
    private void adFullVideoC(){
        //设置广告参数
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(adContentEntity.advertisingId)
                .setSupportDeepLink(true)
                .setImageAcceptedSize(500, 500)
                .setOrientation(TTAdConstant.VERTICAL)
                .setUserID(AppConfig.AD_USER_ID)
                .build();
        //加载全屏视频
        mTTAdNative.loadFullScreenVideoAd(adSlot, new TTAdNative.FullScreenVideoAdListener() {
            @Override
            public void onError(int code, String message) {
                LogUtils.e("onError loadFullScreenVideoAd"+",message="+message+",code="+code);
                if (adListener != null){
                    adListener.onFail();
                }
            }

            @Override
            public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ad) {
                mttFullVideoAd = ad;
                mttFullVideoAd.setFullScreenVideoAdInteractionListener(new TTFullScreenVideoAd.FullScreenVideoAdInteractionListener() {
                    @Override
                    public void onAdShow() {

                    }

                    @Override
                    public void onAdVideoBarClick() {
                    }

                    @Override
                    public void onAdClose() {
                        if (adListener != null){
                            adListener.onVideoComplete();
                        }
                    }

                    @Override
                    public void onVideoComplete() {
                        if (adListener != null){
                            adListener.onVideoComplete();
                        }
                    }

                    @Override
                    public void onSkippedVideo() {
                    }

                });
                mttFullVideoAd.showFullScreenVideoAd(weakReference.get());
            }

            @Override
            public void onFullScreenVideoCached() {
            }

        });
    }



    /**
     * 清空数据
     */
    public void clear(){
        if (mttFullVideoAd != null) {
            mttFullVideoAd = null;
        }
        mTTAdNative = null;
    }
}
